package sdufe.lele.controller;

import org.apache.shiro.util.ThreadContext;
import org.springframework.web.bind.annotation.*;
import sdufe.lele.annotation.authToken;
import sdufe.lele.dao.AccountTypeMapper;
import sdufe.lele.model.AccountType;
import sdufe.lele.result.ApiResult;
import sdufe.lele.result.MapResult;
import sdufe.lele.result.Result;
import sdufe.lele.service.UpdateTypeService;


import javax.annotation.Resource;
import java.util.List;

/**
 * Created by zhnh9 on 4/15/2017.
 */
@RestController
@RequestMapping("/type")
public class TypeController {

    @Resource
    private UpdateTypeService updateTypeService;


    @Resource
    private AccountTypeMapper accountTypeMapper;


    /**
     * 更新 记账类型 信息
     *
     * @param oldName
     * @param newName
     * @return
     */
    @PutMapping("/updateNewName")
    @authToken
    public Result updateNewName(@RequestParam("oldName") String oldName,
                                @RequestParam("newName") String newName) {
        ApiResult apiResult = new ApiResult();
        updateTypeService.updateAll(oldName, newName);
        return apiResult;

    }

    /**
     * 获取所有的 记账类型
     *
     * @return
     */
    @GetMapping("/selectAll.do")
    @authToken
    public Result getAccountType() {
        MapResult mapResult = new MapResult();

        String phone = (String) ThreadContext.get("user-phone");
        List<AccountType> accountTypeList;
        if (phone.equals("admin")) {
            accountTypeList = accountTypeMapper.selectAll();
        } else {
            accountTypeList = accountTypeMapper.selectAllByPhone(phone);
        }

        mapResult.put("list", accountTypeList);
        return mapResult;

    }


    /**
     * 收入/指出 记账类型
     *
     * @return
     */
    @GetMapping("/selectByType.do")
    @authToken
    public Result getAccountTypeByType(@RequestParam(name = "type") Integer type) {
        MapResult mapResult = new MapResult();
        String phone = (String) ThreadContext.get("user-phone");
        List<AccountType> accountTypeList;
        if (phone.equals("admin")) {
            accountTypeList = accountTypeMapper.selectInOut(type);
        } else {
            accountTypeList = accountTypeMapper.selectInOutByPhone(type, (String) ThreadContext.get("user-phone"));
        }

        mapResult.put("list", accountTypeList);
        return mapResult;
    }


    /**
     * 添加一个记账类型
     *
     * @param accountTypeName 记账类型名称
     * @param inOut           表明是收入类型还是支出类型
     * @return 成功只返回status
     */
    @PostMapping("/insert.do")
    @authToken
    public Result insertAccountType(@RequestParam(name = "accountTypeName") String accountTypeName,
                                    @RequestParam(name = "inOut") Integer inOut) {
        ApiResult apiResult = new ApiResult();
        AccountType accountType = new AccountType(accountTypeName, inOut, (String) ThreadContext.get("user-phone"));
        accountTypeMapper.insertSelective(accountType);
        return apiResult;

    }

    @DeleteMapping("/delete.do")
    @authToken
    public Result deleteAccountType(@RequestParam(name = "accountTypeName") String accountTypeName) {
        ApiResult apiResult = new ApiResult();
        accountTypeMapper.deleteByName(accountTypeName);
        return apiResult;
    }
}
